//
//  DUButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/26.
//

import Cocoa

@IBDesignable
class DUButton: BaseView {
    
    private var monthLabel: DULabel!
    private var data:Int = -1
    var enabled:Bool = false
    var target:PageTypeModel?
    
    
    
    
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize () {
        self.mouseDownStyle = getDownStyle()
        self.mouseEnteredStyle = getEnteredStyle()
        self.defaultStyle = ViewStyleUtil.dayDefaultStyle()
        monthLabel = DULabel()
        monthLabel.frame = NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
        monthLabel.cell = VerticallyCenteredTextFieldCell()
        monthLabel.alignment = .center
        monthLabel.font = NSFont.systemFont(ofSize: 25)
        addSubview(monthLabel)
    }
    
    override func layout() {
        super.layout()
        monthLabel.setFrameSize(NSSize(width: self.frame.width, height: self.frame.height))
    }
    
    func stringValue(_ value:String, data:Int) {
        monthLabel.stringValue = value
        self.data = data
        if data == -1 {
            self.monthLabel.textColor = NSColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1)
        } else {
            self.monthLabel.textColor = NSColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1)
        }
    }
    
    func getStringValue() -> String {
        return monthLabel.stringValue
    }
    
    func defaultSelected() {
        self.defaultSelect = true
        self.selected = true
        self.setStyle(getDownStyle())
    }
    
    func getData() -> Int {
        return data
    }
    
    override func mouseDown(with event: NSEvent) {
        if !enabled {
            super.mouseDown(with: event)
        }
    }
    override func mouseExited(with event: NSEvent) {
        if !enabled {
            super.mouseExited(with: event)
        }
    }
    
    override func mouseEntered(with event: NSEvent) {
        if !enabled {
            super.mouseEntered(with: event)
        }
    }
    
    @IBInspectable var labelValue: String? {
        didSet {
            stringValue("2021", data: 1)
        }
    }
    
    
    private func getDownStyle() -> ViewStyle {
        var s = ViewStyle()
        s.backgroundColor = CGColor(red: 0.3, green: 0.43, blue: 0.95, alpha: 0.5)
        s.borderColor = nil
        s.borderWidth = 0
        s.radius = 8
        return s
    }
    
    private func getEnteredStyle() -> ViewStyle {
        var s = ViewStyle()
        s.backgroundColor = CGColor(red: 0.3, green: 0.43, blue: 0.95, alpha: 0.2)
        s.borderColor = nil
        s.borderWidth = 0
        s.radius = 8
        return s
    }
}
